require "import"
import "java.util.Date"
import "java.util.Calendar"
import "android.icu.util.IslamicCalendar"
import "android.icu.util.CopticCalendar"
import "android.widget.Toast"
import "android.os.SystemClock"
import "android.content.Context"

local PREFS_NAME, context = "HijriDateAdjustmentPrefs", service

local function getPref(key, defaultValue)
  return context and context.getSharedPreferences(PREFS_NAME, 0).getInt(key, defaultValue) or defaultValue
end

local function savePref(key, value)
  if context then context.getSharedPreferences(PREFS_NAME, 0).edit().putInt(key, value).commit() end
end

local options = {
  {0, "الوضع العادي", "Normal mode", "Mode normal"},
  {1, "زيادة يوم واحد", "Plus 1 day", "Plus 1 jour"},
  {2, "زيادة يومين", "Plus 2 days", "Plus 2 jours"},
  {3, "زيادة ٣ أيام", "Plus 3 days", "Plus 3 jours"},
  {-1, "إنقاص يوم واحد", "Minus 1 day", "Moins 1 jour"},
  {-2, "إنقاص يومين", "Minus 2 days", "Moins 2 jours"},
  {-3, "إنقاص ٣ أيام", "Minus 3 days", "Moins 3 jours"}
}
local valueMap = { [0]=1, [1]=2, [2]=3, [3]=4, [-1]=5, [-2]=6, [-3]=7 }

local lastClick = getPref("last_click_time", 0)
local adjustment = getPref("hijri_day_adjustment", 0)

local function getLanguage()
  if not context then return "ar" end
  local locale = context.getResources().getConfiguration().locale
  local localeStr = tostring(locale)
  local localeLang = string.sub(localeStr, 1, 2)
  if localeLang == "fr" then return "fr"
  elseif localeLang == "en" then return "en"
  else return "ar" end
end

function cycleSettings()
  local lang = getLanguage()
  local currentIndex = valueMap[adjustment] or 1
  local nextIndex = (currentIndex % #options) + 1
  adjustment = options[nextIndex][1]
  savePref("hijri_day_adjustment", adjustment)
  
  local msg = ""
  if lang == "fr" then
    msg = "Ajustement hégirien: " .. options[nextIndex][4]
  elseif lang == "en" then
    msg = "Hijri adjustment: " .. options[nextIndex][3]
  else
    msg = "تعديل الهجري: " .. options[nextIndex][2]
  end
  
  service.speak(msg)
  Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
end

function speakAllDates()
  local lang = getLanguage()
  local now = Date()
  
  local cal = Calendar.getInstance()
  cal.setTime(now)
  
  local islamic = IslamicCalendar(now)
  islamic.add(IslamicCalendar.DATE, adjustment) 

  local coptic = CopticCalendar(now)

  local data = {
    ar = {
      days = {"الأَحَد", "الإِثْنَين", "الثُّلَاثَاء", "الأَرْبِعَاء", "الخَمِيس", "الجُمُعَة", "السَّبْت"},
      g = {"يَنَايِر", "فِبْرَايِر", "مَارِس", "أَبْرِيل", "مَايُو", "يُونِيُو", "يُولِيُو", "أَغُسْطُس", "سِبْتَمْبَر", "أُكْتُوبَر", "نُوفَمْبَر", "دِيسَمْبَر"},
      s = {"كَانُون الثَانِي", "شُبَاط", "آذَار", "نِيسَان", "أَيَّار", "حَزِيرَان", "تَمُوز", "آب", "أَيْلُول", "تِشْرِين الأَوَّل", "تِشْرِين الثَانِي", "كَانُون الأَوَّل"},
      h = {"مُحَرَّم", "صَفَر", "رَبِيع الأَوَّل", "رَبِيع الآخِر", "جُمَادَى الأُولَى", "جُمَادَى الآخِرَة", "رَجَب", "شَعْبَان", "رَمَضَان", "شَوَّال", "ذُو القَعْدَة", "ذُو الحِجَّة"},
      c = {"تُوت", "بَابَة", "هَاتُور", "كِيهَك", "طُوبَة", "أَمْشِير", "بَرَمْهَات", "بَرَمُودَة", "بَشْنَس", "بَؤُونَة", "أَبِيب", "مِسْرَى", "النَّسِئ"}
    },
    en = {
      days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
      g = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"},
      s = {"Kanun al-Thani", "Shubat", "Adar", "Nisan", "Ayyar", "Haziran", "Tammuz", "Ab", "Aylul", "Tishrin al-Awwal", "Tishrin al-Thani", "Kanun al-Awwal"},
      h = {"Muharram", "Safar", "Rabi' al-Awwal", "Rabi' al-Thani", "Jumada al-Awwal", "Jumada al-Thani", "Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhu al-Qi'dah", "Dhu al-Hijjah"},
      c = {"Thout", "Babah", "Hator", "Kiahk", "Tubah", "Amshir", "Baramhat", "Baramoudeh", "Bashans", "Baounah", "Abib", "Mesra", "Nasie"}
    },
    fr = {
      days = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"},
      g = {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"},
      s = {"Kanun al-Thani", "Shubat", "Adar", "Nisan", "Ayyar", "Haziran", "Tammuz", "Ab", "Aylul", "Tishrin al-Awwal", "Tishrin al-Thani", "Kanun al-Awwal"},
      h = {"Muharram", "Safar", "Rabi' al-Awwal", "Rabi' al-Thani", "Jumada al-Awwal", "Jumada al-Thani", "Rajab", "Sha'ban", "Ramadan", "Shawwal", "Dhu al-Qi'dah", "Dhu al-Hijjah"},
      c = {"Thout", "Babah", "Hator", "Kiahk", "Tubah", "Amshir", "Baramhat", "Baramoudeh", "Bashans", "Baounah", "Abib", "Mesra", "Nasie"}
    }
  }

  local current = data[lang]
  local gMonthIndex = cal.get(Calendar.MONTH) + 1
  local dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)
  
  local speechText = ""
  if lang == "fr" then
    speechText = "Aujourd'hui c'est " .. current.days[dayOfWeek] .. ", " ..
                 "Calendrier Romain: " .. cal.get(Calendar.DATE) .. " " .. current.g[gMonthIndex] .. " " .. cal.get(Calendar.YEAR) .. ", " ..
                 "Calendrier Syrien: " .. cal.get(Calendar.DATE) .. " " .. current.s[gMonthIndex] .. " " .. cal.get(Calendar.YEAR) .. ", " ..
                 "Calendrier Hégirien: " .. islamic.get(IslamicCalendar.DATE) .. " " .. current.h[islamic.get(IslamicCalendar.MONTH) + 1] .. " " .. islamic.get(IslamicCalendar.YEAR) .. ", " ..
                 "Calendrier Copte: " .. coptic.get(CopticCalendar.DATE) .. " " .. current.c[coptic.get(CopticCalendar.MONTH) + 1] .. " " .. coptic.get(CopticCalendar.YEAR) .. "."
  elseif lang == "en" then
    speechText = "Today is " .. current.days[dayOfWeek] .. ", " ..
                 "Roman Calendar: " .. cal.get(Calendar.DATE) .. " " .. current.g[gMonthIndex] .. " " .. cal.get(Calendar.YEAR) .. ", " ..
                 "Syrian Calendar: " .. cal.get(Calendar.DATE) .. " " .. current.s[gMonthIndex] .. " " .. cal.get(Calendar.YEAR) .. ", " ..
                 "Hijri Calendar: " .. islamic.get(IslamicCalendar.DATE) .. " " .. current.h[islamic.get(IslamicCalendar.MONTH) + 1] .. " " .. islamic.get(IslamicCalendar.YEAR) .. ", " ..
                 "Coptic Calendar: " .. coptic.get(CopticCalendar.DATE) .. " " .. current.c[coptic.get(CopticCalendar.MONTH) + 1] .. " " .. coptic.get(CopticCalendar.YEAR) .. "."
  else
    speechText = "الْيَوم هو " .. current.days[dayOfWeek] .. "، " .. 
                 "الميلادي الروماني: " .. cal.get(Calendar.DATE) .. " " .. current.g[gMonthIndex] .. " " .. cal.get(Calendar.YEAR) .. "، " ..
                 "السرياني الشامي: " .. cal.get(Calendar.DATE) .. " " .. current.s[gMonthIndex] .. " " .. cal.get(Calendar.YEAR) .. "، " ..
                 "الهجري: " .. islamic.get(IslamicCalendar.DATE) .. " " .. current.h[islamic.get(IslamicCalendar.MONTH) + 1] .. " " .. islamic.get(IslamicCalendar.YEAR) .. "، " ..
                 "القبطي: " .. coptic.get(CopticCalendar.DATE) .. " " .. current.c[coptic.get(CopticCalendar.MONTH) + 1] .. " " .. coptic.get(CopticCalendar.YEAR) .. "."
  end

  service.speak(speechText)
end

local now_time = SystemClock.elapsedRealtime()
if now_time - lastClick < 1000 then
  cycleSettings()
  savePref("last_click_time", 0)
else
  speakAllDates()
  savePref("last_click_time", now_time)
end
